-- HealthSystem.lua（增强版）
local HealthSystem = {}
HealthSystem.__index = HealthSystem

function HealthSystem.new()
    local instance = {
        healthBars = {},
        uiRoot = nil,
        characterManager = nil  -- 添加角色管理器引用
    }
    return setmetatable(instance, HealthSystem)
end

-- 设置角色管理器
function HealthSystem:setCharacterManager(characterManager)
    self.characterManager = characterManager
    print("角色管理器已设置到血量系统")
end

function HealthSystem:createHealthBar(characterId, options)
    local HealthBar = require("HealthBar")
    
    print("为 " .. characterId .. " 创建血条")
    
    local healthBar = HealthBar.new(options or {})
    if not healthBar then 
        print("创建HealthBar对象失败")
        return nil 
    end
    
    local uiNode = healthBar:create()
    if not uiNode then 
        print("创建血条节点失败")
        return nil 
    end
    
    self.healthBars[characterId] = healthBar
    
    print("血条创建成功")
    return healthBar
end

function HealthSystem:setHealthBarTarget(characterId, targetNode, offsetX, offsetY)
    local healthBar = self.healthBars[characterId]
    
    if healthBar and targetNode then
        return healthBar:setFollowTarget(targetNode, offsetX, offsetY)
    end
    
    -- 尝试从角色管理器获取目标节点
    if not targetNode and self.characterManager then
        local character = self.characterManager:getCharacter(characterId)
        if character and character.node then
            targetNode = character.node
        end
    end
    
    if healthBar and targetNode then
        return healthBar:setFollowTarget(targetNode, offsetX, offsetY)
    end
    
    print("设置血条目标失败: " .. characterId)
    return false
end

function HealthSystem:updateHealthBar(characterId, currentHealth, maxHealth)
    local healthBar = self.healthBars[characterId]
    
    if healthBar then
        healthBar:updateHealth(currentHealth, maxHealth)
        return true
    end
    
    -- 如果血条不存在，尝试从角色管理器获取血量信息并创建血条
    if self.characterManager then
        local character = self.characterManager:getCharacter(characterId)
        if character then
            local healthBar = self:createHealthBar(characterId, {
                width = character.isEnemy and 120 or 200,
                height = character.isEnemy and 16 or 24,
                offsetY = character.isEnemy and 60 or 80
            })
            
            if healthBar and character.node then
                healthBar:setFollowTarget(character.node, 0, character.isEnemy and 60 or 80)
                healthBar:updateHealth(currentHealth, maxHealth)
                return true
            end
        end
    end
    
    print("更新血条失败，未找到血条: " .. characterId)
    return false
end

-- 移除角色血条
function HealthSystem:removeHealthBar(characterId)
    local healthBar = self.healthBars[characterId]
    if healthBar then
        if healthBar.cleanup then
            healthBar:cleanup()
        end
        self.healthBars[characterId] = nil
        print("移除血条: " .. characterId)
        return true
    end
    return false
end

-- HealthSystem.lua 修改 reset 方法
function HealthSystem:reset()
    for characterId, healthBar in pairs(self.healthBars) do
        if healthBar and healthBar.cleanup then
            healthBar:cleanup()
        end
    end
    self.healthBars = {}
    print("血量系统重置")
end

return HealthSystem